package com.cxcs.cxcsojbackendjudgeservice.rabbitmq;

import com.cxcs.cxcsojbackendjudgeservice.judge.service.JudgeService;
import com.cxcs.cxcsojbackendmodel.entity.QuestionSubmit;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 消息队列 消费者$
 *
 * @author： CxCS
 * @date： 2024/9/10
 * @version: 1.0
 */
@Component
@Slf4j
public class MyMessageConsumer {
    @Resource
    private JudgeService judgeService;

    // 指定程序监听的消息队列和确认机制
    @SneakyThrows
    @RabbitListener(queues = {"code_queue"}, ackMode = "MANUAL")
    public void receiveMessage(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        log.info("receiveMessage message = {}", message);

        try {
            // 题目服务(消息队列生产者)提交 id
            long questionSubbmitId = Long.parseLong(message);
            // todo 消息队列消费者 异步化调用判题方法
            judgeService.doJudge(questionSubbmitId);
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {

            channel.basicNack(deliveryTag, false, false);
        }
    }
}

